home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / solitaire.el < prev    next >
Lisp/Scheme  |  1996-02-17  |  15KB  |  456 lines

  1. ;;; solitaire.el --- game of solitaire in Emacs Lisp
  2.  
  3. ;; Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Jan Schormann <Jan.Schormann@informatik.uni-oldenburg.de>
  6. ;; Created: Fri afternoon, Jun  3,  1994
  7. ;; Keywords: games
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; This mode is for playing a well-known game of solitaire
  29. ;; in which you jump pegs across other pegs.
  30.  
  31. ;; The game itself is somehow self-explanatory.  Read the help text to
  32. ;; solitaire, and try it.
  33.  
  34. ;;; Code:
  35.  
  36. (defvar solitaire-mode-map nil
  37.   "Keymap for playing solitaire.")
  38.  
  39. (if solitaire-mode-map
  40.     ()
  41.   (setq solitaire-mode-map (make-sparse-keymap))
  42.   (suppress-keymap solitaire-mode-map t)
  43.   (define-key solitaire-mode-map "\C-f" 'solitaire-right)
  44.   (define-key solitaire-mode-map "\C-b" 'solitaire-left)
  45.   (define-key solitaire-mode-map "\C-p" 'solitaire-up)
  46.   (define-key solitaire-mode-map "\C-n" 'solitaire-down)
  47.   (define-key solitaire-mode-map [return] 'solitaire-move)
  48.   (substitute-key-definition 'undo 'solitaire-undo
  49.                  solitaire-mode-map global-map)
  50.   (define-key solitaire-mode-map " " 'solitaire-do-check)
  51.   (define-key solitaire-mode-map "q" 'solitaire-quit)
  52.  
  53.   (define-key solitaire-mode-map [right] 'solitaire-right)
  54.   (define-key solitaire-mode-map [left] 'solitaire-left)
  55.   (define-key solitaire-mode-map [up] 'solitaire-up)
  56.   (define-key solitaire-mode-map [down] 'solitaire-down)
  57.  
  58.   (define-key solitaire-mode-map [S-right] 'solitaire-move-right)
  59.   (define-key solitaire-mode-map [S-left]  'solitaire-move-left)
  60.   (define-key solitaire-mode-map [S-up]    'solitaire-move-up)
  61.   (define-key solitaire-mode-map [S-down]  'solitaire-move-down)
  62.  
  63.   (define-key solitaire-mode-map [kp-6] 'solitaire-right)
  64.   (define-key solitaire-mode-map [kp-4] 'solitaire-left)
  65.   (define-key solitaire-mode-map [kp-8] 'solitaire-up)
  66.   (define-key solitaire-mode-map [kp-2] 'solitaire-down)
  67.   (define-key solitaire-mode-map [kp-5] 'solitaire-center-point)
  68.  
  69.   (define-key solitaire-mode-map [S-kp-6] 'solitaire-move-right)
  70.   (define-key solitaire-mode-map [S-kp-4] 'solitaire-move-left) 
  71.   (define-key solitaire-mode-map [S-kp-8] 'solitaire-move-up)   
  72.   (define-key solitaire-mode-map [S-kp-2] 'solitaire-move-down) 
  73.  
  74.   (define-key solitaire-mode-map [kp-enter] 'solitaire-move)
  75.   (define-key solitaire-mode-map [kp-0] 'solitaire-undo)
  76.  
  77.   ;; spoil it with s ;)
  78.   (define-key solitaire-mode-map [?s] 'solitaire-solve)
  79.  
  80.   ;;  (define-key solitaire-mode-map [kp-0] 'solitaire-hint) - Not yet provided ;)
  81.   )
  82.  
  83. ;; Solitaire mode is suitable only for specially formatted data.
  84. (put 'solitaire-mode 'mode-class 'special)
  85.  
  86. (defun solitaire-mode ()
  87.   "Major mode for playing solitaire.
  88. To learn how to play solitaire, see the documentation for function
  89. `solitaire'.
  90. \\<solitaire-mode-map>
  91. The usual mnemonic keys move the cursor around the board; in addition,
  92. \\[solitaire-move] is a prefix character for actually moving a stone on the board."
  93.   (interactive)
  94.   (kill-all-local-variables)
  95.   (use-local-map solitaire-mode-map)
  96.   (setq truncate-lines t)
  97.   (setq major-mode 'solitaire-mode)
  98.   (setq mode-name "Solitaire")
  99.   (run-hooks 'solitaire-mode-hook))
  100.  
  101. (defvar solitaire-stones 0
  102.   "Counter for the stones that are still there.")
  103.  
  104. (defvar solitaire-center nil
  105.   "Center of the board.")
  106.  
  107. (defvar solitaire-start nil
  108.   "Upper left corner of the board.")
  109.  
  110. (defvar solitaire-start-x nil)
  111. (defvar solitaire-start-y nil)
  112.  
  113. (defvar solitaire-end nil
  114.   "Lower right corner of the board.")
  115.  
  116. (defvar solitaire-end-x nil)
  117. (defvar solitaire-end-y nil)
  118.  
  119. (defvar solitaire-auto-eval t
  120.   "*Non-nil means check for possible moves after each major change.
  121. This takes a while, so switch this on if you like to be informed when
  122. the game is over, or off, if you are working on a slow machine.")
  123.  
  124. (defconst solitaire-valid-directions
  125.   '(solitaire-left solitaire-right solitaire-up solitaire-down))
  126.  
  127. ;;;###autoload
  128. (defun solitaire (arg)
  129.   "Play Solitaire.
  130.  
  131. To play Solitaire, type \\[solitaire].
  132. \\<solitaire-mode-map>
  133. Move around the board using the cursor keys.
  134. Move stones using \\[solitaire-move] followed by a direction key.
  135. Undo moves using \\[solitaire-undo].
  136. Check for possible moves using \\[solitaire-do-check].
  137. \(The variable solitaire-auto-eval controls whether to automatically
  138. check after each move or undo)
  139.  
  140. What is Solitaire?
  141.  
  142. I don't know who invented this game, but it seems to be rather old and
  143. it's origin seems be northern Africa.  Here's how to play:
  144. Initially, the board will look similar to this:
  145.  
  146.     Le Solitaire             
  147.     ============             
  148.     
  149.         o   o   o        
  150.     
  151.         o   o   o        
  152.     
  153.     o   o   o   o   o   o   o
  154.     
  155.     o   o   o   .   o   o   o
  156.     
  157.     o   o   o   o   o   o   o
  158.     
  159.         o   o   o        
  160.     
  161.         o   o   o        
  162.  
  163. Let's call the o's stones and the .'s holes.  One stone fits into one
  164. hole.  As you can see, all holes but one are occupied by stones.  The
  165. aim of the game is to get rid of all but one stone, leaving that last
  166. one in the middle of the board if you're cool.
  167.  
  168. A stone can be moved if there is another stone next to it, and a hole
  169. after that one.  Thus there must be three fields in a row, either
  170. horizontally or vertically, up, down, left or right, which look like
  171. this:  o  o  .
  172.  
  173. Then the first stone is moved to the hole, jumping over the second,
  174. which therefore is taken away.  The above thus `evaluates' to:  .  .  o
  175.  
  176. That's all.  Here's the board after two moves:
  177.  
  178.         o   o   o        
  179.     
  180.         .   o   o        
  181.     
  182.     o   o   .   o   o   o   o
  183.     
  184.     o   .   o   o   o   o   o
  185.     
  186.     o   o   o   o   o   o   o
  187.     
  188.         o   o   o        
  189.     
  190.         o   o   o
  191.  
  192. Pick your favourite shortcuts:
  193.  
  194. \\{solitaire-mode-map}"
  195.  
  196.   (interactive "P")
  197.   (switch-to-buffer "*Solitaire*")
  198.   (solitaire-mode)
  199.   (setq buffer-read-only t)
  200.   (setq solitaire-stones 32)
  201.   (solitaire-insert-board)
  202.   (solitaire-build-modeline)
  203.   (goto-char (point-max))
  204.   (setq solitaire-center (search-backward "."))
  205.   (setq buffer-undo-list (list (point)))
  206.   (set-buffer-modified-p nil))
  207.  
  208. (defun solitaire-build-modeline ()
  209.   (setq mode-line-format
  210.     (list "" "---" 'mode-line-buffer-identification
  211.           (if (< 1 solitaire-stones)
  212.           (format "--> There are %d stones left <--" solitaire-stones)
  213.         "------")
  214.           'global-mode-string "   %[(" 'mode-name 'minor-mode-alist "%n"
  215.           ")%]-%-"))
  216.   (force-mode-line-update))
  217.  
  218. (defun solitaire-insert-board ()
  219.   (let* ((buffer-read-only nil)
  220.      (w (window-width))
  221.      (h (window-height))
  222.      (hsep (cond ((> w 26) "   ")
  223.              ((> w 20) " ")
  224.              (t "")))
  225.      (vsep (cond ((> h 17) "\n\n")
  226.              (t "\n")))
  227.      (indent (make-string (/ (- w 7 (* 6 (length hsep))) 2) ?\ )))
  228.     (erase-buffer)
  229.     (insert (make-string (/ (- h 7 (if (> h 12) 3 0)
  230.                    (* 6 (1- (length vsep)))) 2) ?\n))
  231.     (if (or (string= vsep "\n\n") (> h 12))
  232.     (progn
  233.       (insert (format "%sLe Solitaire\n" indent))
  234.       (insert (format "%s============\n\n" indent))))
  235.     (insert indent)
  236.     (setq solitaire-start (point))
  237.     (setq solitaire-start-x (current-column))
  238.     (setq solitaire-start-y (solitaire-current-line))
  239.     (insert (format " %s %so%so%so%s" hsep hsep hsep hsep vsep))
  240.     (insert (format "%s %s %so%so%so%s" indent hsep hsep hsep hsep vsep))
  241.     (insert (format "%so%so%so%so%so%so%so%s" indent hsep hsep hsep hsep hsep hsep vsep))
  242.     (